package com.dyx.source.reactor.singleThreadReactor;

import java.io.IOException;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * Handler 的一种，绑定了 connect 事件。当客户端发起 connect 请求时，Reactor 会将 accept 事件分发给 Acceptor 处理
 */
public class Acceptor implements Runnable {

    private Selector selector;

    private ServerSocketChannel serverSocket;

    public Acceptor(Selector selector, ServerSocketChannel serverSocket) {
        this.selector = selector;
        this.serverSocket = serverSocket;
    }

    public void run() {
        try {
            //接受client连接请求
            SocketChannel channel = serverSocket.accept();
            if (channel != null) {
                // 然后交给handler处理
                new Handler(selector, channel);
            }
        } catch (IOException ex) { /* ... */ }
    }
}